package com.luntan.activity;


import com.baidu.mapapi.SDKInitializer;
import com.luntan.view.R;

import android.app.TabActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

public class MainActivity extends TabActivity implements OnClickListener {
	private static String Tab_Home = "zhuye";
	private static String Tab_Account = "zhanghu";
	private static String Tab_Suggestion = "yijian";
	private static String Tab_Set = "shezhi";
	private static String Tab_More = "gengduo";
	private static TabHost mTabHost;
	private static final int COLOR1 = Color.parseColor("#787878");
	private static final int COLOR2 = Color.parseColor("#ffffff");
	private ImageView mBut1, mBut2, mBut3, mBut4, mBut5;
	private TextView mText1, mText2, mText3, mText4, mText5;

	private Intent mHomeItent, mAccountIntent, mSuggestionIntent, mSetIntent,
			mMoreIntent;
	private int mCurTabId = R.id.channel1;

	// Animation
	private Animation left_in, left_out;
	private Animation right_in, right_out;

	/**
	 * 构造广播监听类，监听 SDK key 验证以及网络异常广播
	 */
	public class SDKReceiver extends BroadcastReceiver {
		public void onReceive(Context context, Intent intent) {
			String s = intent.getAction();
			if (s.equals(SDKInitializer.SDK_BROADTCAST_ACTION_STRING_PERMISSION_CHECK_ERROR)) {
//				text.setText("key 验证出错! 请在 AndroidManifest.xml 文件中检查 key 设置");
			} else if (s
					.equals(SDKInitializer.SDK_BROADCAST_ACTION_STRING_NETWORK_ERROR)) {
//				text.setText("网络出错");
			}
		}
	}

	private SDKReceiver mReceiver;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		SDKInitializer.initialize(getApplicationContext()); 
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		// 注册 SDK 广播监听者
		IntentFilter iFilter = new IntentFilter();
		iFilter.addAction(SDKInitializer.SDK_BROADTCAST_ACTION_STRING_PERMISSION_CHECK_ERROR);
		iFilter.addAction(SDKInitializer.SDK_BROADCAST_ACTION_STRING_NETWORK_ERROR);
		mReceiver = new SDKReceiver();
		registerReceiver(mReceiver, iFilter);
		
		prepareAnim();
		prepareIntent();
		setupIntent();
		prepareView();
	}
	
	private void prepareAnim() {
		left_in = AnimationUtils.loadAnimation(this, R.anim.left_in);
		left_out = AnimationUtils.loadAnimation(this, R.anim.left_out);

		right_in = AnimationUtils.loadAnimation(this, R.anim.right_in);
		right_out = AnimationUtils.loadAnimation(this, R.anim.right_out);
	}

	private void prepareIntent() {
		mHomeItent = new Intent(this, WellJobActivity.class);
		mAccountIntent = new Intent(this, FindWaiterActivity.class);
		mSuggestionIntent = new Intent(this, GoForumActivity.class);
		mSetIntent = new Intent(this, AboutUsActivity.class);
	}
	
	private void prepareView() {
		mBut1 = (ImageView) findViewById(R.id.imageView1);
		mBut2 = (ImageView) findViewById(R.id.imageView2);
		mBut3 = (ImageView) findViewById(R.id.imageView3);
		mBut4 = (ImageView) findViewById(R.id.imageView4);
		findViewById(R.id.channel1).setOnClickListener(this);
		findViewById(R.id.channel2).setOnClickListener(this);
		findViewById(R.id.channel3).setOnClickListener(this);
		findViewById(R.id.channel4).setOnClickListener(this);
		mText1 = (TextView) findViewById(R.id.textView1);
		mText2 = (TextView) findViewById(R.id.textView2);
		mText3 = (TextView) findViewById(R.id.textView3);
		mText4 = (TextView) findViewById(R.id.textView4);
	}
	private void setupIntent() {
		mTabHost = getTabHost();
		mTabHost.addTab(buildTabSpec(Tab_Home, R.string.category_home,
				R.drawable.icon_1, mHomeItent));
		mTabHost.addTab(buildTabSpec(Tab_Account,
				R.string.category_set, R.drawable.icon_2, mAccountIntent));
		mTabHost.addTab(buildTabSpec(Tab_Suggestion, R.string.category_search,
				R.drawable.icon_3, mSuggestionIntent));
		mTabHost.addTab(buildTabSpec(Tab_Set,
				R.string.category_account, R.drawable.icon_4, mSetIntent));
		
	}
	private TabHost.TabSpec buildTabSpec(String tag, int resLabel, int resIcon,
			final Intent content) {
		return mTabHost
				.newTabSpec(tag)
				.setIndicator(getString(resLabel),
						getResources().getDrawable(resIcon))
				.setContent(content);
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			mBut1.performClick();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
	
	public static void setCurrentTabByTag(String tab) {
		mTabHost.setCurrentTabByTag(tab);
	}
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (mCurTabId == v.getId()) {
			return;
		}
		mBut1.setImageResource(R.drawable.icon_1);
		mBut2.setImageResource(R.drawable.icon_2);
		mBut3.setImageResource(R.drawable.icon_3);
		mBut4.setImageResource(R.drawable.icon_4);
		mText1.setTextColor(COLOR1);
		mText2.setTextColor(COLOR1);
		mText3.setTextColor(COLOR1);
		mText4.setTextColor(COLOR1);
		int checkedId = v.getId();
		final boolean o;
		if (mCurTabId < checkedId)
			o = true;
		else
			o = false;
		if (o)
			mTabHost.getCurrentView().startAnimation(left_out);
		else
			mTabHost.getCurrentView().startAnimation(right_out);
		switch (checkedId) {
		case R.id.channel1:
			mTabHost.setCurrentTabByTag(Tab_Home);
			mBut1.setImageResource(R.drawable.icon_1);
			mText1.setTextColor(COLOR2);
			break;
		case R.id.channel2:
			mTabHost.setCurrentTabByTag(Tab_Account);
			mBut2.setImageResource(R.drawable.icon_2);
			mText2.setTextColor(COLOR2);
			break;
		case R.id.channel3:
			mTabHost.setCurrentTabByTag(Tab_Suggestion);
			mBut3.setImageResource(R.drawable.icon_3);
			mText3.setTextColor(COLOR2);
			break;
		case R.id.channel4:
			mTabHost.setCurrentTabByTag(Tab_Set);
			mBut4.setImageResource(R.drawable.icon_4);
			mText4.setTextColor(COLOR2);
			break;
		default:
			break;
		}

		if (o)
			mTabHost.getCurrentView().startAnimation(left_in);
		else
			mTabHost.getCurrentView().startAnimation(right_in);
		mCurTabId = checkedId;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	/* (non-Javadoc)
	 * @see android.app.ActivityGroup#onDestroy()
	 */
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		// 取消监听 SDK 广播
	    unregisterReceiver(mReceiver);
		super.onDestroy();
	}

}
